package com.its.common.disruptor.producer;

import com.its.common.disruptor.dto.TranslatorData;
import com.its.common.disruptor.dto.TranslatorDataWapper;
import com.lmax.disruptor.RingBuffer;

/**
 * @Author xiaxp
 * @Date 2020-08-06T18:39:54.729
 * @Description
 */
public class MessageProducer {

	private String producerId;
	
	private RingBuffer<TranslatorDataWapper> ringBuffer;
	
	public MessageProducer(String producerId, RingBuffer<TranslatorDataWapper> ringBuffer) {
		this.producerId = producerId;
		this.ringBuffer = ringBuffer;
	}
	
	public void onData(TranslatorData data) {
		long sequence = ringBuffer.next();
		try {
			TranslatorDataWapper wapper = ringBuffer.get(sequence);
			wapper.setData(data);
		} finally {
			ringBuffer.publish(sequence);
		}
	}

}
